package tree;

import java.util.ArrayList;
import java.util.List;

/**
 * @ Author     ：侯耀祖
 * @ Description：
 */
public class hasPathSum {
    Boolean result = false;
    public boolean hasPathSum(TreeNode root, int targetSum) {
        if (root == null){
            return false;
        }
        if (root.left == null && root.right == null){
            return root.val == targetSum;
        }
        return hasPathSum(root.left, targetSum - root.val) || hasPathSum(root.right, targetSum - root.val);
    }
    public void getPath(TreeNode root, List<Integer> record, int t){
        if (root == null){
            return;
        }
        record.add(root.val);
        getPath(root.left, record, t);
        getPath(root.right, record, t);
        if (root.left == null && root.right == null){
            if (record.stream()
                    .reduce(0, Integer::sum) == t){
                result = true;
                return;
            }
            record.remove(record.size()-1);
        } else {
            record.remove(record.size()-1);
        }
    }
}
